<?php
namespace common\service;
use common\models\ActiveAttr;
use common\models\AttributeValue;
use common\models\GoodsAttr;
class GoodsService extends BaseService
{
    /**
     * 获取商品SKU
     * $type 1 过滤库存0
     * @param $id
     * @return mixed
     */
    public static function GetAttrlist($id,$type=0,$aids=0){
        $lists = GoodsAttr::find();
        $lists->andWhere(['goods_id'=>$id,'is_del'=>0]);
        if($type == 1){
            $lists->andWhere(['>','inventory',0]);
        }

        if($aids > 0){
            $activeattr = ActiveAttr::find()->where(['active_id'=>$aids])->asArray()->all();
            $newattr = [];
            foreach ($activeattr as $attr){
                $newattr[$attr['goods_attre_id']] = $attr;
            }
        }

        $list = $lists->asArray()->all();

        if($list) {
            $aid = explode('-', $list[0]['attribute_value_id']);
            $attrtitle = AttributeValue::find()->where(['in', 'id', $aid])->orderBy(['id' => SORT_ASC])->asArray()->all();
            $vid = array_column($list, 'attribute_value_id');

            $newdata['thead'] = array_column($attrtitle, 'attr_name');
            $newiddata = [];
            foreach ($vid as $val) {
                $newid = explode('-', $val);
                foreach ($newid as $vid) {
                    if ($vid > 0) {
                        $newiddata[] = $vid;
                    }
                }
            }
            $newiddata = array_unique($newiddata);

            $newvaldata = AttributeValue::find()->where(['in', 'id', $newiddata])->asArray()->all();
            $valdatanew = array();
            foreach ($newvaldata as $newdats) {
                $valdatanew[$newdats['id']] = $newdats;
            }

            $data = [];
            foreach ($list as $lval) {
                $newavid = explode('-', $lval['attribute_value_id']);
                $newvalue['original_price'] = $lval['original_price'];
                $newvalue['discount_price'] = $lval['discount_price'];
                $newvalue['one_price'] = $lval['one_price'];
                $newvalue['inventory'] = $lval['inventory'];
                $newvalue['image'] = GlobalUrlService::buildCdnUrl($lval['image']);
                $newvalue['images'] = $lval['image'];
                $newvalue['attribute_value_id'] = $lval['attribute_value_id'];
                $newvalue['attr_id'] = $lval['id'];
                $newvalue['id'] = $lval['id'];
                if($aids > 0){

                    if($lval['id'] == $newattr[$lval['id']]['goods_attre_id']){
                        $newvalue['checked'] = 1;
                    } else {
                        $newvalue['checked'] = 0;
                    }
                    $newvalue['inventory'] = $lval['inventory'] + $newattr[$lval['id']]['inventory'];
                }
                $namedata = array();
                $attrvaldata = array();
                foreach ($newavid as $newval) {
                    if ($newval > 0) {
                        $namedata[] = $valdatanew[$newval];
                        $attrvaldata[] = $valdatanew[$newval]['value'];
                    }

                }
                $newvalue['attr_value'] = implode(':', $attrvaldata);
                $newvalue['val'] = $namedata;
                $data[] = $newvalue;
            }
            $newdata['tbody'] = $data;
            return $newdata;
        } else{
            return array();
        }
    }

    /**
     * 获取属性值
     * @param string $attribute_value_id
     * @return string
     */
    public static function getCurrentArrt($active_attr_id = []){
        if(empty($active_attr_id)){
            return '';
        }
        //获取所有的属性
        $actives = ActiveAttr::find()->where(['id'=>$active_attr_id])->asArray()->all();
        $attrids = '';
        $new_actives = [];
        foreach ($actives as $value){
            $new_actives[$value['id']] = $value['attribute_value_id'];
            $ids = str_replace('-',',',$value['attribute_value_id']);
            $attrids = $attrids.','.$ids ;
        }
        unset($value);
        $attrids = explode(',',$attrids);
        $attrids = array_splice($attrids,1);
        $new_attrids = array_unique($attrids);

        //获取所有属性值
        $attributes = AttributeValue::find()->where(['id'=>$new_attrids])->asArray()->all();
        if(empty($attributes)){
            return '';
        }

        //组合属性名称和值
        $new_attributes = [];
        foreach ($attributes as $val){
            $new_attributes[$val['id']] = $val['attr_name'].':'.$val['value'];

        }
        unset($val);

        //组合属性id和对应的值
        $active_attr_val = [];
        foreach ($new_actives as $key=>$val){
            $attr_id = explode('-',$val);
            $str = '';

            foreach ($attr_id as $id){
                if(isset($new_attributes[$id]) && !empty($new_attributes[$id])){
                    $str = $str.','.$new_attributes[$id];
                }
            }
            $active_attr_val[$key] = ltrim($str,',');
        }
        return $active_attr_val;
    }
}